package ysq.xyg.sale.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.DateUtil;
import ysq.xyg.sale.dao.SaleRecordDao;
import ysq.xyg.sale.model.po.SaleProduct;
import ysq.xyg.sale.model.po.SaleProductMy;
import ysq.xyg.sale.model.po.SaleRecord;
import ysq.xyg.sale.util.SaleBaseContent;
import ysq.xyg.sys.model.po.SysUser;
import ysq.xyg.sale.model.vo.SaleRecordVo;
import ysq.xyg.sale.service.SaleRecordService;
import ysq.xyg.sys.service.SysUserService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


/**
 * 销售模块-流水记录service实现
 *
 * @author shengQiang yu
 * @date 2019-9-24 16:02:37
 */
@Service("saleRecordService")
public class SaleRecordServiceImpl extends BaseServiceImpl<SaleRecord> implements SaleRecordService {
    @Resource
    private SysUserService sysUserService;
    @Resource
    private SaleRecordDao saleRecordDao;

    private SaleRecord saleRecord;

    @Autowired
    public void setBaseDao(SaleRecordDao saleRecordDao) {
        this.baseDao = saleRecordDao;
    }

    /**
     * 管理员充值费用
     *
     * @param userId 充值对象id
     * @param money 充值金额
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnEntity managerAddMoney(String userId,Double money, SysUser currentUser) {
        SysUser sysUser=sysUserService.selectByPrimaryKey(currentUser.getId());
        if(sysUser==null){
            return new ReturnEntity(BaseConstant.FAIL,"用户信息错误");
        }
        SysUser receptUser=sysUserService.selectByPrimaryKey(userId);
        if(receptUser==null){
            return new ReturnEntity(BaseConstant.FAIL,"充值用户用户信息错误");
        }
        //创建我的充值流水
        SaleRecord saleRecord=new SaleRecord(sysUser);
        saleRecord.setUserId(userId);
        saleRecord.setType(SaleBaseContent.SALE_RECORD_TYPE_CZ);
        saleRecord.setMoney(money+"");
        saleRecord.setExtendId("");
        saleRecord.setRemarks("管理员给你充值"+money+"元。");
        int saleRecordId=insert(saleRecord);
        if(saleRecordId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的充值流水失败！");
        }
        //添加账号充值金额
        receptUser.setMoney(receptUser.getMoney()+money);
        int usesId=sysUserService.updateByPrimaryKey(receptUser);
        if(usesId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"添加账号充值金额失败！");
        }
        return new ReturnEntity();
    }
    /**
     * 上级代理充值费用
     *
     * @param userId 充值对象id
     * @param money 充值金额
     * @param currentUser 当前用户
     * @param request
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-9 14:17:04
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public ReturnEntity parentAddMoney(HttpServletRequest request, String userId,Double money, SysUser currentUser) {
        SysUser sysUser=sysUserService.selectByPrimaryKey(currentUser.getId());
        if(sysUser==null){
            return new ReturnEntity(BaseConstant.FAIL,"用户信息错误");
        }
        SysUser receptUser=sysUserService.selectByPrimaryKey(userId);
        if(receptUser==null){
            return new ReturnEntity(BaseConstant.FAIL,"充值用户用户信息错误");
        }
        if(sysUser.getMoney()<money){
            return new ReturnEntity(BaseConstant.FAIL,"本次充值"+money+"元，您的账户只有"+sysUser.getMoney()+"元。您的余额不足！");
        }
        //创建我的充值流水
        SaleRecord saleRecord=new SaleRecord(sysUser);
        saleRecord.setUserId(receptUser.getId());
        saleRecord.setType(SaleBaseContent.SALE_RECORD_TYPE_CZ);
        saleRecord.setMoney(money+"");
        saleRecord.setExtendId("");
        saleRecord.setRemarks("登录名为["+sysUser.getLoginName()+"]给你充值"+money+"元。");
        int saleRecordId=insert(saleRecord);
        if(saleRecordId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的充值流水失败！");
        }
        //添加账号充值金额
        receptUser.setMoney(receptUser.getMoney()+money);
        int usesId=sysUserService.updateByPrimaryKey(receptUser);
        if(usesId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"添加账号充值金额失败！");
        }

        //创建我的消费流水
        SaleRecord saleRecords=new SaleRecord(sysUser);
        saleRecords.setUserId(sysUser.getId());
        saleRecords.setType(SaleBaseContent.SALE_RECORD_TYPE_XF);
        saleRecords.setMoney(money+"");
        saleRecords.setExtendId(saleRecord.getId());
        saleRecords.setRemarks("给登录名为["+receptUser.getLoginName()+"]充值"+money+"元。");
        int saleRecordsId=insert(saleRecords);
        if(saleRecordsId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"创建我的消费流水失败！");
        }
        //减去账号消费金额
        sysUser.setMoney(sysUser.getMoney()-money);
        int usersId=sysUserService.updateByPrimaryKey(sysUser);
        if(usesId<=0){
            return new ReturnEntity(BaseConstant.FAIL,"减去账户消费金额失败！");
        }else{
            request.getSession().setAttribute(BaseConstant.SESSION_CURRENT_USER,sysUser);
        }
        return new ReturnEntity();
    }
    /**
     * 分页查询
     *
     * @param saleRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-24 16:02:37
     */
    @Override
    public ReturnEntity page(SaleRecordVo saleRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SaleRecord> list = saleRecordDao.listLeft(saleRecordVo);
        PageInfo<SaleRecord> page=new PageInfo<SaleRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 我的流程分页查询
     *
     * @param saleRecordVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-24 16:02:37
     */
    @Override
    public ReturnEntity pageByMy(SaleRecordVo saleRecordVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<SaleRecord> list = saleRecordDao.listLeftByMy(saleRecordVo);
        PageInfo<SaleRecord> page=new PageInfo<SaleRecord>(list);
        return new ReturnEntity(page.getList(),page.getTotal(),page.isHasNextPage());
    }
    /**
     * 修改
     *
     * @param saleRecordVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-24 16:02:37
     */
    @Override
    public ReturnEntity addOrUpdate(SaleRecordVo saleRecordVo, SysUser currentUser) {
        boolean isUpate=true;
        if(StrUtil.isEmpty(saleRecordVo.getId())){
            saleRecord=new SaleRecord(currentUser);
            isUpate=false;
        }else{
            saleRecord=selectByPrimaryKey(saleRecordVo.getId());
            if(ObjectUtil.isNull(saleRecord)){
                return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
            }
            saleRecord.setUpdateTime(new Date());
            saleRecord.setUpdateUser(currentUser.getId());
        }
        saleRecord.setUserId(saleRecordVo.getUserId());
        saleRecord.setType(saleRecordVo.getType());
        saleRecord.setMoney(saleRecordVo.getMoney());
        saleRecord.setExtendId(saleRecordVo.getExtendId());
        saleRecord.setRemarks(saleRecordVo.getRemarks());
        int num=0;
        if(isUpate){
            num=updateByPrimaryKey(saleRecord);
        }else{
            num=insert(saleRecord);
        }
        if(num<=0){
            return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }
    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author shengQiang yu
     * @date 2019-9-24 16:02:37
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        SaleRecord saleRecord=selectByPrimaryKey(id);
        if(saleRecord!=null){
            saleRecord.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            saleRecord.setUpdateTime(new Date());
            saleRecord.setUpdateUser(currentUser.getId());
            int num=updateByPrimaryKey(saleRecord);
            if(num>0){
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL,BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param saleRecordVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author shengQiang yu
     * @date 2019-9-24 16:02:37
     */
    @Override
    public List<SaleRecord> acceptList(SaleRecordVo saleRecordVo){
        Example example = new Example(SaleRecord.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(saleRecordVo.getRemoveTag())){
            criteria.andEqualTo("removeTag",saleRecordVo.getRemoveTag());
        }
        example.setOrderByClause(" update_time desc");
        return saleRecordDao.selectByExample(example);
    }
}
